package com.activiti.global;

import com.activiti.bean.Evection;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.task.Task;

import java.util.HashMap;
import java.util.Map;

/**
 * 带判断条件的流程分支
 *
 * @author ldanniel
 */
public class ProcessArgument {
    public static void main(String[] args) {
        ProcessArgument processArgument = new ProcessArgument();
        processArgument.commitProcess();
    }
    public void deployment(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment()
                //禁用架构验证
                .disableSchemaValidation()
                .name("出差申请流程-myVariables")
                .addClasspathResource("bpmn/evection-global.bpmn")
                .deploy();
        System.out.println("部署流程的id" + deploy.getId());
        //部署流程的名称为空，是因为压缩包中可能有多个流程图，所以不能统一命名
        System.out.println("部署流程的名称" + deploy.getName());
    }
    public void startProcess(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //设置流程变量
        String key = "myVariables";
        Map<String,Object> variablesMap = new HashMap<>();
        Evection evection = new Evection();
        evection.setNum(2d);
        variablesMap.put("evection",evection);
        variablesMap.put("assignee0","关羽");
        variablesMap.put("assignee1","张飞");
        variablesMap.put("assignee2","刘备");
        variablesMap.put("assignee3","诸葛亮");
        //启动流程
        runtimeService.startProcessInstanceByKey(key,variablesMap);
    }

    /**
     * 第一个分支，小于等于3天
     */
    public void commitProcess(){
        //创建
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        //通过任务服务接口获取，参数：流程id,负责人
        Task task = taskService.createTaskQuery()
                .processInstanceId("20001")
                .taskAssignee("张飞")
                .singleResult();
        System.out.println("流程id = " + task.getProcessInstanceId());
        System.out.println("任务id = " + task.getId());
        System.out.println("负责人 = " + task.getAssignee());
        System.out.println("任务名称 = " + task.getName());
        //根据任务id完成任务
        taskService.complete(task.getId());
        System.out.println("执行任务完成");
    }
}
